<?php

class EnquiryRequest extends DataObject implements PermissionProvider {
    private static $singular_name = "Enquiry Request";
    private static $plural_name = "Enquiry Requests";
    
    private static $email_to = '';
    private static $email_template = '';
    
    private static $db = array(
        'Type' => "Enum(array('General Enquiry', 'Support Enquiry'))",
        'Name' => 'Varchar(100)',
        'Email' => 'Varchar(100)',
        'Phone' => 'Varchar(20)',
        'Message' => 'Text',
        'Remark' => 'Text'
    );
    
    private static $default_sort = "Created DESC";
    
    private static $searchable_fields = array(
        'Type',
        'Created' => array(
			'field' => 'DateField',
			'filter' => 'DateMatchFilter'
		),
        'Name',
        'Email',
        'Phone',
        'Remark'
    );

    private static $summary_fields = array(
        'Type',
        'Name',
        'Email',
        'Phone',
        'Message',
        'Remark'
    );
	
	public function fieldLabels($includerelations = true) {
		$labels = parent::fieldLabels($includerelations);
		
		$labels['Type'] = _t('EnquiryRequest.TYPE', 'Type');
		$labels['Name'] = _t('EnquiryRequest.NAME', 'Name');
		$labels['Email'] = _t('EnquiryRequest.EMAIL', 'Email');
		$labels['Phone'] = _t('EnquiryRequest.PHONE', 'Phone');
		$labels['Message'] = _t('EnquiryRequest.MESSAGE', 'Message');
		$labels['Remark'] = _t('EnquiryRequest.REMARK', 'Remark');
		
		return $labels;	
	}
    
    function getCMSFields() {
        $fields = parent::getCMSFields();

        if($this->ID){
            $fields->makeFieldReadonly('Type');
            $fields->makeFieldReadonly('Name');
            $fields->makeFieldReadonly('Email');
            $fields->makeFieldReadonly('Phone');
            $fields->makeFieldReadonly('Message');
        }

        return $fields;
    }
    
    function getFrontEndFields($params = null) {
        $fields = parent::getFrontendFields($params);
        $fields->dataFieldByName('Type')->setEmptyString(_t('EnquiryRequest.PLEASE_SELECT', 'Please select...'));
        $fields->removeByName('Remark');

        return $fields;
    }
    
    function send(){
        if($this->Type == 'General Enquiry'){
            $email_to = SiteConfig::current_site_config()->CompanyGeneralEmail ? SiteConfig::current_site_config()->CompanyGeneralEmail : $this->config()->email_to;
            if($email_to){
                $email_from = sprintf("%s <%s>", $this->Name, $this->Email);
                $body = sprintf("<p>You've an enquiry request from user. See below for the details.</p><p>Name: %s</p><p>Email: %s</p><p>Phone: %s</p><p>Message: %s</p>", $this->Name, $this->Email, $this->Phone, $this->Message);
                $e = new Email($email_from, $email_to, 'Enquiry Request', $body);
                $e->populateTemplate($this);
                if($this->config()->email_template){
                    $e->setTemplate($this->config()->email_template);
                }
                $e->send();
            }
        }
        else if($this->Type == 'Support Enquiry'){
            $email_to = SiteConfig::current_site_config()->CompanySupportEmail ? SiteConfig::current_site_config()->CompanySupportEmail : $this->config()->email_to;
            if($email_to){
                $email_from = sprintf("%s <%s>", $this->Name, $this->Email);
                $body = sprintf("<p>You've an enquiry request from user. See below for the details.</p><p>Name: %s</p><p>Email: %s</p><p>Phone: %s</p><p>Message: %s</p>", $this->Name, $this->Email, $this->Phone, $this->Message);
                $e = new Email($email_from, $email_to, 'Enquiry Request', $body);
                $e->populateTemplate($this);
                if($this->config()->email_template){
                    $e->setTemplate($this->config()->email_template);
                }
                $e->send();
            }
        }
    }

	function canView($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('VIEW_EnquiryRequest');
    }

    function canEdit($member = false) {
        $extended = $this->extendedCan(__FUNCTION__, $member);
        if($extended !== null) {
            return $extended;
        }
        return Permission::check('EDIT_EnquiryRequest');
    }

    function canDelete($member = false) {
        return false;
    }

    function canCreate($member = false) {
        return false;
    }

    public function providePermissions() {
        return array(
            'VIEW_EnquiryRequest' => array(
                'name' => _t('EnquiryRequest.PERMISSION_VIEW', 'Allow view access right'),
                'category' => _t('EnquiryRequest.PERMISSIONS_CATEGORY', 'Enquiry Request')
            ),
            'EDIT_EnquiryRequest' => array(
                'name' => _t('EnquiryRequest.PERMISSION_EDIT', 'Allow edit access right'),
                'category' => _t('EnquiryRequest.PERMISSIONS_CATEGORY', 'Enquiry Request')
            )
        );
    }
}
?>